/*
 *  Copyright (C) 2010 Interactive Media Management
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.i2m.converge.plugins.videooutput;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaTool;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.Global;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * API for outputting graphics.
 *
 * To get this code to work Xuggle must be installed on the machine.
 *
 * Once installed you must configure the environment.
 *
 * MacOSX:
 *
 * export XUGGLE_HOME=/usr/local/xuggler/       # Location where Zuggle was installed
 * export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$XUGGLE_HOME/lib    # Xuggle's dynamic libraries
 *
 * @author Allan Lykke Christensen
 */
public class VideoOutputter {

    private List<VideoNewsItem> newsItems = new ArrayList<VideoNewsItem>();

    public void generate(List<VideoNewsItem> newsItems, int secsPerStory, String bgVideo, String outputVideo) {
        this.newsItems = newsItems;

        IContainer readContainer = IContainer.make();
        if (readContainer.open(bgVideo, IContainer.Type.READ, null) < 0) {
            throw new IllegalArgumentException("Couldn't read " + bgVideo);
        }

        IMediaReader reader = ToolFactory.makeReader(readContainer);
        IMediaWriter writer = ToolFactory.makeWriter(outputVideo, reader);

        reader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);

        IMediaTool newsItemOverlay = new NewsItemOverlay(this.newsItems, 1000000 * secsPerStory);
        reader.addListener(newsItemOverlay);
        reader.addListener(writer);


        // read packets from the source file, which dispatch events to the
        // writer, this will continue until the end of the source file

        while (reader.readPacket() == null) {
            do {
            } while (false);
        }
        readContainer.close();
    }

    public void getInfo(String file) {
        IContainer container = IContainer.make();
        container.open(file, IContainer.Type.READ, null);
        System.out.println("Streams: " + container.getNumStreams());

        for (int i = 0; i < container.getNumStreams(); i++) {
            IStream stream = container.getStream(i);
            IStreamCoder coder = stream.getStreamCoder();

            System.out.printf("stream %d: ", i);
            System.out.println("");
            System.out.printf("type: %s; ", coder.getCodecType());
            System.out.println("");
            System.out.printf("codec: %s; ", coder.getCodecID());
            System.out.println("");
            System.out.printf("duration: %s; ", stream.getDuration() == Global.NO_PTS ? "unknown" : "" + stream.getDuration());
            System.out.println("");
            System.out.printf("start time: %s; ", container.getStartTime() == Global.NO_PTS ? "unknown" : "" + stream.getStartTime());
            System.out.println("");
            System.out.printf("language: %s; ", stream.getLanguage() == null ? "unknown" : stream.getLanguage());
            System.out.println("");
            System.out.printf("timebase: %d/%d; ", stream.getTimeBase().getNumerator(), stream.getTimeBase().getDenominator());
            System.out.println("");
            System.out.printf("coder tb: %d/%d; ", coder.getTimeBase().getNumerator(), coder.getTimeBase().getDenominator());
            System.out.println("");

            if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
                System.out.printf("sample rate: %d; ", coder.getSampleRate());
                System.out.println("");
                System.out.printf("channels: %d; ", coder.getChannels());
                System.out.println("");
                System.out.printf("format: %s", coder.getSampleFormat());
                System.out.println("");
            } else if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
                System.out.printf("width: %d; ", coder.getWidth());
                System.out.println("");
                System.out.printf("height: %d; ", coder.getHeight());
                System.out.println("");
                System.out.printf("format: %s; ", coder.getPixelType());
                System.out.println("");
                System.out.printf("frame-rate: %5.2f; ", coder.getFrameRate().getDouble());
                System.out.println("");
            }
            System.out.println("");
        }
        System.out.println("");
    }
}
